package com.klod.news.page

import android.annotation.SuppressLint
import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import androidx.navigation.createGraph
import com.klod.news.config.RouteConfig
import com.klod.news.page.home.HomePage
import com.klod.news.page.home.MessagePage
import com.klod.news.page.home.PublishPage
import com.klod.news.page.home.RecommendPage
import com.klod.news.page.home.UserPage
import com.klod.news.widget.BottomNavItems

@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@Composable
fun MainScreen() {
    val navController = rememberNavController()
    Scaffold(
        modifier = Modifier
//                        .windowInsetsPadding(WindowInsets.statusBars)
        ,
        bottomBar = {


            val navBackStackEntry by navController.currentBackStackEntryAsState()
            val currentDestination = navBackStackEntry?.destination
            //底部栏
            BottomAppBar(
                containerColor = MaterialTheme.colorScheme.primaryContainer,
            ) {
                BottomNavItems.forEach { item ->
                    val isSelect =
                        currentDestination?.hierarchy?.any { it.route == item.route } == true
                    NavigationBarItem(
                        icon = {
                            Icon(
                                if (isSelect) item.icon else item.unselectedIcon,
                                contentDescription = item.name
                            )
                        },
                        label = { Text(item.name) },
                        selected = isSelect,
                        onClick = {
                            navController.navigate(item.route) {
                                // 弹出到图表的起始目标，以避免在用户选择项目时
                                // 在返回堆栈上构建大量目标
                                popUpTo(navController.graph.findStartDestination().id) {
                                    saveState = true
                                }
                                // 重新选择同一项目时，避免同一目标的多个副本
                                launchSingleTop = true
                                // 重新选择先前选择的项目时，恢复状态
                                restoreState = true
                            }
                        }
                    )
                }

            }
        }) {

        val navGraph =
            navController.createGraph(startDestination = RouteConfig.ROUTE_HOME) {
                // 在这里定义您的导航路线
                composable(RouteConfig.ROUTE_HOME) { HomePage() }
                composable(RouteConfig.ROUTE_RECOMMEND) { RecommendPage() }
                composable(RouteConfig.ROUTE_PUBLISH) { PublishPage() }
                composable(RouteConfig.ROUTE_MESSAGE) { MessagePage() }
                composable(RouteConfig.ROUTE_USER) { UserPage() }
            }
        NavHost(
            navController = navController,
            graph = navGraph,

            )


    }


}